package linkedlist;


public class DoubleLinkedListDemo {

    static DoubleLinkedList doubleLinkedList = new DoubleLinkedList();

    public static void main(String[] args) {
        //进行测试
        //先创建节点
        HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头");

        //加入
        doubleLinkedList.add(hero1);
        doubleLinkedList.add(hero2);
        doubleLinkedList.add(hero3);
        doubleLinkedList.add(hero4);

        //查看
        System.out.println("原来链表的情况~~");
        doubleLinkedList.list();

        //修改
        HeroNode2 newHero = new HeroNode2(4,"张三","法外狂徒");
        doubleLinkedList.update(newHero);
        System.out.println("修改后的链表");
        doubleLinkedList.list();

        //删除
        doubleLinkedList.delete(3);
        System.out.println("删除后");
        doubleLinkedList.list();

        //按序号插入
        System.out.println("按条件插入");
        HeroNode2 newHero2 = new HeroNode2(1,"有用","智多星");
        doubleLinkedList.addByOrder(newHero2);
        doubleLinkedList.list();
    }

}

class DoubleLinkedList{
    private HeroNode2 head = new HeroNode2(0,"","");

    public HeroNode2 getHead() {
        return head;
    }

    //遍历，显示链表
    public void list(){
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        while(temp != null){
            System.out.println(temp);
            temp = temp.next;
        }
    }

    //在链表后面添加方法
    public void add(HeroNode2 node){
        //temp不能为head.next,否则初次添加前链表的head.next为空,到赋值时就会出现head.next.next = node
        //中间存了一个null，会报异常
        HeroNode2 temp = head;
        while(temp.next != null){
            temp = temp.next;
        }
        temp.next = node;
        node.pre = temp;
    }

    //排序添加方法
    /*
    * 为了避免空指针异常，在取节点元素时，必须避开头节点
    * */
    public void addByOrder(HeroNode2 node){

        HeroNode2 temp = head;
        HeroNode2 temp2 = head;
        //遍历拿到最后一个节点
        while(temp.next != null){
            if (temp.next.no == node.no){
                System.out.println("要插入的元素已存在");
                return;
            }
           temp = temp.next;
        }
        //判断要插入的节点是否比最后节点要大
        if (node.no > temp.no){
            temp.next = node;
            node.pre = temp;
        }
        while(temp2.next != null){
            if (temp2.next.no > node.no){
                //先把要插入节点的前后指向定义好，否则会出现空指针异常
                node.next = temp2.next;
                node.pre = temp2;
                temp2.next = node;
                temp2.next.pre = node;
                break;
            }
            temp2 = temp2.next;
        }
    }

    //修改节点
    public void update(HeroNode2 newNode){
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        boolean flag = false;
        HeroNode2 temp = head.next;
        while(temp != null){
            if (temp.no == newNode.no){
                temp.name = newNode.name;
                temp.nickname = newNode.nickname;
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            System.out.println(temp.no + "节点已修改");
        }else{
            System.out.println("没有找到需要修改的节点");
        }
    }

    //删除节点
    public void delete(int no){
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while(temp != null){
            if (temp.no == no){
                temp.pre.next = temp.next;
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            System.out.println(temp.no + "节点已删除");
        }else{
            System.out.println("没有找到需要删除的节点");
        }
    }

}


//定义HeroNode ， 每个HeroNode 对象就是一个节点
class HeroNode2 {
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next; //指向下一个节点
    public HeroNode2 pre;
    //构造器
    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }
    //为了显示方法，我们重新toString


    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}